package com.xiaoyu.izf.controller.api;

import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.exception.BusinessException;
import com.xiaoyu.izf.model.exception.BusinessStatusEnum;
import com.xiaoyu.izf.model.page.FormResult;
import com.xiaoyu.izf.service.VipOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/vip")
public class VipOrderApiController extends BaseApiController{
    @Autowired
    private VipOrderService orderService;

    /**
     *
     * @param request
     * @param vip 用户指定要买或者升级（不能降级）的vip类型 1、2、3
     * @return
     */
    @RequestMapping("/order")
    public FormResult<String> orderGet(HttpServletRequest request, int vip){
        UserInfo user = getUserByToken(request);
        if (vip==21||vip==23){
            throw new BusinessException(BusinessStatusEnum.ERROR,"省级或区级服务商不支持线上购买或升级。");
        }else if (vip==3&&user.getVipType()!=0){
            throw new BusinessException(BusinessStatusEnum.ERROR,"vip等级相等或者不能降级。");
        }else if (vip==2&&!(user.getVipType()==3||user.getVipType()==0)){
            throw new BusinessException(BusinessStatusEnum.ERROR,"vip等级相等或者不能降级。");
        }else if (vip==1&&!(user.getVipType()==0||user.getVipType()==2||user.getVipType()==3)){
            throw new BusinessException(BusinessStatusEnum.ERROR,"vip等级相等或者不能降级。");
        }
        String orderId = "v"+System.currentTimeMillis() + (long) (Math.random() * 10000000L);
        orderService.orderAdd(orderId,vip,user);
        return new FormResult<>(BusinessStatusEnum.OK,orderId);
    }
}
